package com.haixiaoke.flat.repairs.controller;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.repairs.domain.Repairs;
import com.haixiaoke.saas.repairs.service.IRepairsService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报修Controller
 * 
 * @author panda
 * @date 2025-07-05
 */
@RestController
@RequestMapping("/front/flat/repairs/repairs")
public class FlatRepairsController extends BaseController {
    @Autowired
    private IRepairsService repairsService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询报修列表
     */
    @AuthCheck
    @GetMapping("/page")
    public TableDataInfo list(Repairs repairs) {
        startPage();
        repairs.setConUserId(frontTokenComponent.getUserIdNotNull());
        List<Repairs> list = repairsService.selectRepairsList(repairs);
        return getDataTable(list);
    }

    /**
     * 获取报修详细信息
     */
    @GetMapping(value = "/{repairsId}")
    public AjaxResult getInfo(@PathVariable("repairsId") Long repairsId) {
        return success(repairsService.selectRepairsByRepairsId(repairsId));
    }

    /**
     * 新增报修
     */
    @PostMapping
    public AjaxResult add(@RequestBody Repairs repairs) {
        repairs.setCreateBy(frontTokenComponent.getLoginUser().getUser().getUserName());
        repairs.setConUserId(frontTokenComponent.getUserIdNotNull());
        return repairsService.insertRepairs(repairs);
    }

    /**
     * 验收报修
     */
    @PutMapping("/check/{repairsId}")
    public AjaxResult edit(@PathVariable Long repairsId) {
        return repairsService.checkRepairs(repairsId, frontTokenComponent.getLoginUser().getUser().getUserName());
    }
}
